<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="客户端ID" prop="clientId">
                <el-input
                    clearable
                    v-model="clientId"
                    placeholder="客户端ID"
                ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ buttons.search.name }}
                </el-button>
                <el-button @click="resetForm(queryForm, params, getDataList)">
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: current,
                pageSize: size,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    title="弹窗式"
                    v-permission="['add']"
                    size="small"
                    type="primary"
                    @click="handleEdit(buttons.add.name)"
                >
                    {{ buttons.add.name }}
                </el-button>
            </template>
            <el-table-column
                prop="targetClientId"
                label="客户端ID"
            ></el-table-column>
            <el-table-column prop="clientTargetIp" label="目标客户端地址">
            </el-table-column>
            <el-table-column prop="clientTargetPort" label="目标客户端端口">
            </el-table-column>
            <el-table-column prop="visitorPort" label="访问端口">
            </el-table-column>
            <el-table-column prop="describe" label="描述"> </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
            </el-table-column>
            <el-table-column prop="updateTime" label="修改时间">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['edit']"
                        @click.prevent="handleEdit(buttons.edit.name, row)"
                        type="primary"
                        size="small"
                    >
                        {{ buttons.edit.name }}
                    </el-button>
                    <el-button
                        v-permission="['view']"
                        @click.prevent="href2VisitorPort(row)"
                        type="primary"
                        size="small"
                    >
                        {{ buttons.view.name }}
                    </el-button>
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row)"
                        type="danger"
                        size="small"
                    >
                        {{ buttons.del.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>
        <!-- 编辑组件 -->
        <server-permeate-client-net-work-mapping-edit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="handelDialog($event)"
        />
    </div>
</template>
<script>
import cloudNetworkServerPermeateMenu from "@/views/pages/cloud_network_server_permeate/components/CloudNetworkServerPermeateMenu";

export default {
    data: () => ({
        description: "服务端渗透客户端",
        buttons: {
            search: { name: "查询" },
            add: { name: "添加" },
            edit: { name: "编辑" },
            del: { name: "删除" },
            view: { name: "访客端口预览" },
        },
        // type 0:目录 1：菜单 2：按钮
        type: "1",
        icon: "Connection",
        name: "服务端渗透客户端",
        parentMenu: cloudNetworkServerPermeateMenu,
    }),
};
</script>

<script setup>
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";
//?导入公共查询方法
import {
    onSubmit,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/pages/common";
import ServerPermeateClientNetWorkMappingEdit from "@/views/pages/cloud_network_server_permeate/components/ServerPermeateClientNetWorkMappingEdit.vue";

const { proxy } = getCurrentInstance();
const queryForm = ref(null);
const tableData = ref([]);
const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);

const params = reactive({
    clientId: "",
    size: 10,
    current: 1,
    total: 0,
});
const { clientId, size, current, total } = toRefs(params);

/**
 * 跳转到访客端口
 * @param row
 */
const href2VisitorPort = (row) => {
    // window.location = "https://www.baidu.com";
    console.log(window.location.hostname);
    console.log(window.location.host);
    window.open(
        window.location.protocol +
            "//" +
            window.location.hostname +
            ":" +
            row.visitorPort,
        "_blank",
    ); // 第二个参数'_blank'表示在新窗口中打开链接
};
/**
 * @description: dialog事件
 * @param {*}
 * @return {*}
 */
const handelDialog = (e) => {
    showDialog.value = e;
    getDataList();
};
/**
 * @description:添加or编辑事件
 * @param {*}
 * @return {*}
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**删除行数据
 * @description:
 * @param {*}
 * @return {*}
 */
const handleDel = (row) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } =
                await VE_API.cloudNetwork.networkMappingDelete(row);
            if (code === 0) {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};
/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } = await VE_API.cloudNetwork.networkMappingPage(params);
    if (code === 0) {
        const { size, current, total, records } = data;
        params.size = size;
        params.current = current;
        params.total = total;
        tableData.value = records;
    }
};

onMounted(async () => {
    await getDataList();
});
</script>

<style lang="scss" scoped></style>
